/**
 * Project: Mobile Asset Manager
 * Author: Sergey Budaev
 * Created: 19.12.2007 19:20:58
 * <p/>
 * Copyright (c) 1999-2007 Magenta Corporation Ltd. All Rights Reserved.
 * Magenta Technology proprietary and confidential.
 * Use is subject to license terms.
 */
package com.magenta.maxoptra.dto;

import com.magenta.maxoptra.utils.Point;

import java.io.Serializable;

public class Polygon implements Serializable {
    private static final long serialVersionUID = 8004442038150490177L;

    private Point[] points;
    private Double minLongitude;
    private Double maxLongitude;
    private Double minLatitude;
    private Double maxLatitude;

    public Polygon() {
    }

    public Polygon(String polygonAsString) {
        //27.566721430409707, -81.8701171875|26.194876675795218, -80.09033203125|27.858503954841247, -80.6011962890625|27.955591004642553, -81.837158203125|27.644606381943326, -81.617431640625|27.566721430409707, -81.8701171875
        final Polygon polygon = importPolygon(polygonAsString);
        setPoints(polygon.getPoints());
    }

    public Point[] getPoints() {
        return points;
    }

    public void setPoints(Point[] points) {
        if (points == null || points.length < 3) {
            throw new IllegalArgumentException("Polygon contains less than 3 segments");
        }
        this.points = points;
        minLatitude = points[0].getLatitude();
        maxLatitude = points[0].getLatitude();
        minLongitude = points[0].getLongitude();
        maxLongitude = points[0].getLongitude();

        expandBounds(points);
    }

    public void expandBounds(Point[] points) {
        for (int i = 1; i < points.length; i++) {
            final Point point = points[i];
            if (point.getLatitude() < minLatitude) {
                minLatitude = point.getLatitude();
            } else if (point.getLatitude() > maxLatitude) {
                maxLatitude = point.getLatitude();
            }
            if (point.getLongitude() < minLongitude) {
                minLongitude = point.getLongitude();
            } else if (point.getLongitude() > maxLongitude) {
                maxLongitude = point.getLongitude();
            }
        }
    }

    public Double getMinLongitude() {
        return minLongitude;
    }

    public Double getMaxLongitude() {
        return maxLongitude;
    }

    public Double getMinLatitude() {
        return minLatitude;
    }

    public Double getMaxLatitude() {
        return maxLatitude;
    }

    public String toString() {
        final StringBuilder result = new StringBuilder();
        if (points != null) {
            for (Point point : points) {
                result.append(point.getLatitude())
                        .append(", ")
                        .append(point.getLongitude()).
                        append("|");
            }
            return result.deleteCharAt(result.length() - 1).toString();
        }
        return "";
    }

    public static Polygon importPolygon(String strPolygon) {
        final Polygon polygon = new Polygon();
        final String[] coords = strPolygon.replace("|", ";").split(";");
        final Point[] points = new Point[coords.length];

        for (int i = 0; i < coords.length; i++) {
            final String[] coord = coords[i].split(", ");

            final String latitude = coord[0];
            final String longitude = coord[1];

            final Point point = new Point(Double.parseDouble(longitude), Double.parseDouble(latitude));

            points[i] = point;
        }
        polygon.setPoints(points);
        return polygon;
    }

}